//
//  FanLayout.m
//  us
//
//  Created by karry on 2025/5/13.
//

#import "FanLayout.h"

@implementation FanLayout


- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSMutableArray *attributes = [NSMutableArray array];
    
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    CGFloat radius = 400; // 圆弧半径
    CGFloat centerY = self.collectionView.bounds.size.height * 0.75;
    CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.bounds.size.width / 2;
    CGFloat totalAngle = M_PI; // 180°
    
    for (NSInteger i = 0; i < count; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        
        CGFloat progress = (CGFloat)i / (count - 1);
        CGFloat angle = -M_PI_2 + totalAngle * progress;
        
        CGFloat x = centerX + radius * cos(angle);
        CGFloat y = centerY + radius * sin(angle);
        
        attr.size = CGSizeMake(60, 100);
        attr.center = CGPointMake(x, y);
        attr.transform = CGAffineTransformMakeRotation(angle);
        [attributes addObject:attr];
    }
    
    return attributes;
}

- (CGSize)collectionViewContentSize {
    return CGSizeMake(self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
}

@end
